home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / dfue / elcheapofax / faxcmd / libfax / rcs / log.c,v < prev    next >
Text File  |  1995-03-09  |  5KB  |  241 lines

  1. head    1.3;
  2. access;
  3. symbols
  4.     OCT93:1.3;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.3
  10. date    93.06.11.23.22.07;    author Rhialto;    state Exp;
  11. branches;
  12. next    1.2;
  13.  
  14. 1.2
  15. date    93.06.11.16.15.25;    author Rhialto;    state Exp;
  16. branches;
  17. next    1.1;
  18.  
  19. 1.1
  20. date    93.06.11.15.19.27;    author Rhialto;    state Exp;
  21. branches;
  22. next    ;
  23.  
  24.  
  25. desc
  26. @Log debugging info, diagnostics, errors etc.
  27. @
  28.  
  29.  
  30. 1.3
  31. log
  32. @Print control characters correctly.
  33. @
  34. text
  35. @/* $Id: log.c,v 1.2 1993/06/11 16:15:25 Rhialto Exp $
  36.  * $Log: log.c,v $
  37.  * Revision 1.2  1993/06/11  16:15:25  Rhialto
  38.  * First real RCS checkin
  39.  *
  40.  */
  41. /*
  42.  * This file is part of El Cheapo Fax. All modifications relative to the
  43.  * base source are (C) Copyright 1993 by Olaf 'Rhialto' Seibert.
  44.  * All rights reserved. The GNU General Public License applies.
  45.  */
  46. /*
  47.   This file is part of the NetFax system.
  48.  
  49.   (c) Copyright 1989 by David M. Siegel and Sundar Narasimhan.
  50.       All rights reserved.
  51.  
  52.     This program is free software; you can redistribute it and/or modify
  53.     it under the terms of the GNU General Public License as published by
  54.     the Free Software Foundation.
  55.  
  56.     This program is distributed in the hope that it will be useful,
  57.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  58.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  59.     GNU General Public License for more details.
  60.  
  61.     You should have received a copy of the GNU General Public License
  62.     along with this program; if not, write to the Free Software
  63.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  64. */
  65.  
  66. #include <stdio.h>
  67. #include <stdarg.h>
  68. #include <string.h>
  69.  
  70. #include "c2proto.h"
  71. #include "log.h"
  72.  
  73. static void fix_format(char *format_in, char *format_out, int len, int errno_save);
  74.  
  75. extern int errno;
  76. static int log_level = LOG_DEBUG;
  77. static FILE *logfile;
  78.  
  79. /*
  80.  * We break this routine out from below to avoid performing
  81.  * this scan if we're not using the format string.  That is,
  82.  * the loglevel is too low to force the printout.
  83.  */
  84. static void fix_format(format_in, format_out, len, errno_save)
  85.      char *format_in;
  86.      char *format_out;
  87.      int len;
  88.      int errno_save;
  89. {
  90.     char *f = format_in;
  91.     char *b = format_out;
  92.     char *b_end = &format_out[len];
  93.     char c;
  94.  
  95.     while ((c = *f++) != '\0' && b < b_end) {
  96.     switch (c) {
  97.       case '%':
  98.         c = *f++;
  99.         switch (c) {
  100.           case 'm':
  101.         sprintf(b, "error %d", errno_save);
  102.         b += strlen(b);
  103.         break;
  104.           default:
  105.         *b++ = '%';
  106.         *b++ = c;
  107.         break;
  108.         }
  109.         break;
  110.       default:
  111.         *b++ = c;
  112.         break;
  113.     }
  114.     }
  115.  
  116.     *b = '\0';
  117. }
  118.  
  119. /* VARARGS */
  120. int log(int level, char *file, int line, char *format, ...)
  121. {
  122.     va_list ap;
  123.     static char formatbuf[BUFSIZ];
  124.     static char finalbuf[BUFSIZ];
  125.     static char outbuf[BUFSIZ];
  126.     char *outbuf_ptr = outbuf;
  127.     char *slash, *ptr;
  128.     int errno_save = errno;
  129.  
  130.     if (level > log_level)
  131.       return (0);
  132.  
  133.     if ((slash = strrchr(file, '/')) != NULL)
  134.       file = slash+1;
  135.  
  136.     va_start(ap, format);
  137.  
  138.     fix_format(format, formatbuf, sizeof(formatbuf), errno_save);
  139.     fprintf(stdout, "(%.30s:%d) ", file, line);
  140.     if (logfile)
  141.     fprintf(logfile, "(%.30s:%d) ", file, line);
  142.     vsprintf(finalbuf, formatbuf, ap);
  143.  
  144.     for (ptr = finalbuf; *ptr != NULL; ptr++) {
  145.     switch (*ptr) {
  146.       case '\r':
  147.         *(outbuf_ptr++) = '\\';
  148.         *(outbuf_ptr++) = 'r';
  149.         break;
  150.       case '\n':
  151.         *(outbuf_ptr++) = '\\';
  152.         *(outbuf_ptr++) = 'n';
  153.         break;
  154.       case ETX:
  155.         strcpy(outbuf_ptr, "<ETX>");
  156.         outbuf_ptr += 5;
  157.         break;
  158.       case DLE:
  159.         strcpy(outbuf_ptr, "<DLE>");
  160.         outbuf_ptr += 5;
  161.         break;
  162.       case XON:
  163.         strcpy(outbuf_ptr, "<XON>");
  164.         outbuf_ptr += 5;
  165.         break;
  166.       case XOFF:
  167.         strcpy(outbuf_ptr, "<XOF>");
  168.         outbuf_ptr += 5;
  169.         break;
  170.       case CAN:
  171.         strcpy(outbuf_ptr, "<CAN>");
  172.         outbuf_ptr += 5;
  173.         break;
  174.       default:
  175.         if (*ptr < 32) {
  176.         sprintf(outbuf_ptr, "<^%c>", *ptr + 64);
  177.         outbuf_ptr += 4;
  178.         } else
  179.         *(outbuf_ptr++) = *ptr;
  180.         break;
  181.     }
  182.     }
  183.     *(outbuf_ptr++) = '\n';
  184.     *(outbuf_ptr++) = '\0';
  185.  
  186.     fputs(outbuf, stdout);
  187.     if (logfile)
  188.     fputs(outbuf, logfile);
  189.  
  190.     va_end(ap);
  191.  
  192.     return (0);
  193. }
  194.  
  195. void log_set_level(level)
  196.      int level;
  197. {
  198.     if (level >= 10) {
  199.     if (logfile == NULL)
  200.         logfile = fopen("faxlog", "a");
  201.     level -= 10;
  202.     }
  203.     log_level = level;
  204. }
  205. @
  206.  
  207.  
  208. 1.2
  209. log
  210. @First real RCS checkin
  211. @
  212. text
  213. @d1 5
  214. a5 2
  215. /* $Id$
  216.  * $Log$
  217. d142 1
  218. a142 1
  219.         sprintf(outbuf_ptr, "<^%c>", *ptr + 32);
  220. @
  221.  
  222.  
  223. 1.1
  224. log
  225. @Initial revision
  226. @
  227. text
  228. @d1 3
  229. d40 1
  230. a101 1
  231.     /*fprintf(stderr, "(%.30s:%d) ", file, line);*/
  232. d103 2
  233. d138 5
  234. a142 1
  235.         *(outbuf_ptr++) = *ptr;
  236. a148 1
  237.     /*fputs(outbuf, stderr);*/
  238. d150 2
  239. d161 5
  240. @
  241.